package com.openbravo.data.loader.serialize;

import com.openbravo.data.basic.BasicException;
import e.odbo.data.dsl.query.QBFCompareEnum;

import java.math.BigDecimal;
import java.sql.Blob;
import java.sql.Clob;

public class QBFParameterDataWrite implements DataWrite {

    private String[] m_asFindFields;
    private QBFCompareEnum[] m_aiCondFields;
    private String[] m_aParams;

    public QBFParameterDataWrite(String[] asFindFields) {
        m_asFindFields = asFindFields;
        m_aiCondFields = new QBFCompareEnum[asFindFields.length];
        m_aParams = new String[asFindFields.length];

        for (int i = 0; i < m_aParams.length; i++) {
            m_aiCondFields[i]=QBFCompareEnum.COMP_EQUALS;
            m_aParams[i] = DataWriteUtils.getSQLValue((Object) null);
        }
    }

    public void setDouble(int paramIndex, Double dValue) throws BasicException {
        if ((paramIndex - 1) % 2 == 0) {
            throw new BasicException("exception.nocompare", true);
        } else {
            m_aParams[(paramIndex - 1) / 2] = DataWriteUtils.getSQLValue(dValue);
        }
    }

    public void setBoolean(int paramIndex, Boolean bValue) throws BasicException {
        if ((paramIndex - 1) % 2 == 0) {
            throw new BasicException("exception.nocompare", true);
        } else {
            m_aParams[(paramIndex - 1) / 2] = DataWriteUtils.getSQLValue(bValue);
        }
    }

    public void setInt(int paramIndex, Integer iValue) throws BasicException {
        if ((paramIndex - 1) % 2 == 0) {
            throw new BasicException("exception.nocompare", true);
        } else {
            m_aParams[(paramIndex - 1) / 2] = DataWriteUtils.getSQLValue(iValue);
        }
    }

    @Override
    public void setInteger(int paramIndex, Integer iValue) throws BasicException {
        setInt(paramIndex,iValue);
    }

    public void setString(int paramIndex, String sValue) throws BasicException {
        if ((paramIndex - 1) % 2 == 0) {
            throw new BasicException("exception.nocompare", true);
        } else {
            m_aParams[(paramIndex - 1) / 2] = DataWriteUtils.getSQLValue(sValue);
        }
    }

    public void setTimestamp(int paramIndex, java.util.Date dValue) throws BasicException {
        if ((paramIndex - 1) % 2 == 0) {
            throw new BasicException("exception.nocompare", true);
        } else {
            m_aParams[(paramIndex - 1) / 2] = DataWriteUtils.getSQLValue(dValue);
        }
    }

    //        public void setBinaryStream(int paramIndex, java.serializer.InputStream in, int length) throws DataException{
//            if ((paramIndex - 1) % 2 == 0) {
//                throw new DataException("Expected comparator for QBF");
//            } else {
//                throw new DataException("Param type not allowed");
//            }
//        }
    public void setBytes(int paramIndex, byte[] value) throws BasicException {
        if ((paramIndex - 1) % 2 == 0) {
            throw new BasicException("exception.nocompare", true);
        } else {
            throw new BasicException("Param type not allowed");
        }
    }

    public void setObject(int paramIndex, Object value) throws BasicException {
        if ((paramIndex - 1) % 2 == 0) {
            if (value instanceof QBFCompareEnum) {
                m_aiCondFields[(paramIndex - 1) / 2] = (QBFCompareEnum) value;
            } else {
                throw new BasicException("exception.nocompare", true);
            }
        } else {
            m_aParams[(paramIndex - 1) / 2] = DataWriteUtils.getSQLValue(value);
        }
    }

    public void setObjects(int paramIndex, Object[] values) throws BasicException {
        if ((paramIndex - 1) % 2 == 0) {
            throw new BasicException("exception.nocompare", true);
        } else {
            m_aParams[(paramIndex - 1) / 2] = DataWriteUtils.getSQLValue(values);
        }
    }

    @Override
    public void setLong(int paramIndex, Long lValue) throws BasicException {
        if ((paramIndex - 1) % 2 == 0) {
            throw new BasicException("exception.nocompare", true);
        } else {
            m_aParams[(paramIndex - 1) / 2] = DataWriteUtils.getSQLValue(lValue);
        }

    }

    @Override
    public void setShort(int paramIndex, Short fValue) throws BasicException {
        if ((paramIndex - 1) % 2 == 0) {
            throw new BasicException("exception.nocompare", true);
        } else {
            m_aParams[(paramIndex - 1) / 2] = DataWriteUtils.getSQLValue(fValue);
        }
    }

    @Override
    public void setFloat(int paramIndex, Float fValue) throws BasicException {
        if ((paramIndex - 1) % 2 == 0) {
            throw new BasicException("exception.nocompare", true);
        } else {
            m_aParams[(paramIndex - 1) / 2] = DataWriteUtils.getSQLValue(fValue);
        }
    }

    @Override
    public void setBigDecimal(int paramIndex, BigDecimal bigDecimalValue) throws BasicException {
        if ((paramIndex - 1) % 2 == 0) {
            throw new BasicException("exception.nocompare", true);
        } else {
            m_aParams[(paramIndex - 1) / 2] = DataWriteUtils.getSQLValue(bigDecimalValue);
        }
    }

    @Override
    public void setClob(int paramIndex, Clob clobValue) throws BasicException {
        throw new BasicException("Clob  type not support in qbf");
    }

    @Override
    public void setBlob(int paramIndex, Blob blobValue) throws BasicException {
        throw new BasicException("Blob type not support in qbf");
    }

    public String[] getM_asFindFields() {
        return m_asFindFields;
    }

    public QBFCompareEnum[] getM_aiCondFields() {
        return m_aiCondFields;
    }

    public String[] getM_aParams() {
        return m_aParams;
    }
}
